package com.lingnan.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lingnan.eduservice.entity.po.EduChapterPO;
import com.lingnan.eduservice.entity.po.EduVideoPO;
import com.lingnan.eduservice.entity.vo.ChapterVO;
import com.lingnan.eduservice.entity.vo.VideoVO;
import com.lingnan.eduservice.mapper.EduChapterMapper;
import com.lingnan.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lingnan.eduservice.service.EduVideoService;
import com.lingnan.servicebase.excetioncontrooller.LingNanException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author zzd
 * @since 2021-08-17
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapterPO> implements EduChapterService {

    @Autowired
    private EduVideoService eduVideoService;

    /**
     * 查询当前课程章节和小结(树型)
     *
     * @param courseId 课程号
     * @return
     */
    @Override
    public List<ChapterVO> findChapterVideoByCourseId(String courseId) {
        // 1 查询当前课程下所有章节信息

        // 1.1 创建查询条件
        QueryWrapper<EduChapterPO> chapterWrapper = new QueryWrapper<>();
        // 1.2 查询条件
        chapterWrapper.eq("course_id", courseId);
        // 1.3 执行查询
        List<EduChapterPO> eduChapterPOList = this.baseMapper.selectList(chapterWrapper);

        // 2 查询当前课程下所有小结信息
        // 2.1 创建查询条件
        QueryWrapper<EduVideoPO> videoWrapper = new QueryWrapper<>();
        // 2.2 查询条件
        videoWrapper.eq("course_id", courseId);
        // 2.3 执行查询
        List<EduVideoPO> eduVideoPOList = eduVideoService.list(videoWrapper);


        // 封装章节信息(中转站)
        ChapterVO chapterVO = null;
        // 封装小结信息(中转站)
        VideoVO videoVO = null;
        // 封装小结集合信息(中转站)
        List<VideoVO> videoVOList = null;
        // 封装最终信息
        List<ChapterVO> chapterVOList = new ArrayList<>(100);

        // 3 根据查询条件进行封装
        // 3.1 遍历所有的章节信息
        for (EduChapterPO eduChapter : eduChapterPOList) {

            chapterVO = new ChapterVO();
            videoVOList = new ArrayList<>(100);

            // 存储当前章节
            BeanUtils.copyProperties(eduChapter, chapterVO);

            // 3.2 获取当前章节id
            String chapterId = eduChapter.getId();

            // 3.3 遍历所有的小结信息
            for (EduVideoPO eduVideoPO : eduVideoPOList) {

                videoVO = new VideoVO();
                // 3.4 获取当前小结信息的章节id
                String chapterIdVideo = eduVideoPO.getChapterId();

                if (Objects.equals(chapterId, chapterIdVideo)) {
                    // 3.5 如果属于当前章节的小节
                    // 添加到数据存储中
                    BeanUtils.copyProperties(eduVideoPO, videoVO);
                    videoVOList.add(videoVO);
                }
            }
            chapterVO.setVideoVOList(videoVOList);
            // 存储到最终地方
            chapterVOList.add(chapterVO);

        }


        return chapterVOList;
    }

    /**
     * 根据章节ID删除章节信息
     *
     * @param chapterId
     */
    @Override
    public boolean deleteChapterById(String chapterId) {
        // 根据课程章节ID查询小结信息
        QueryWrapper<EduVideoPO> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        // 执行查询
        int count = eduVideoService.count(wrapper);

        if (count > 0) {
            // 当前章节有小节数据
            throw new LingNanException(20001,"务必先清空小节,再删除章节信息!");
        }
        // 没有小节数据
        int i = baseMapper.deleteById(chapterId);

        // 成功删除则返回true 删除失败返回false
        return i > 0;

    }
    /**
     * 根据课程ID删除小节信息
     * @param courseId
     */
    @Override
    public void deleteChapterByCourseId(String courseId) {
        // 创建查询条件
        QueryWrapper<EduChapterPO> wrapper = new QueryWrapper<>();

        wrapper.eq("course_id",courseId);

        // 执行删除
        baseMapper.delete(wrapper);
    }
}
